package br.edu.ufcg.dsc.domain;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="loan")
public class Loan {

	private int id;
	
	private Unit unit;
	
	private User user;
	
	private Date startDate, devolutionDate;
	
	private boolean open;
	
	public Loan() {
		long now = System.currentTimeMillis();
		this.startDate = new Date(now);
		this.open = true;
		this.devolutionDate = null;
	}
	
	public Loan(User user, Unit unit, int days) {
		long now = System.currentTimeMillis();
		
		this.user = user;
		this.unit = unit;
		
		this.startDate = new Date(now);
		this.open = true;
		this.devolutionDate = null;
	}
	
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne
	@JoinColumn(name = "unit_id")
	public Unit getUnit() {
		return unit;
	}

	public void setUnit(Unit unit) {
		this.unit = unit;
	}

	@ManyToOne(cascade = CascadeType.PERSIST)
	@JoinColumn(name = "user_id")
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Temporal(TemporalType.DATE)
	@Column
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	@Column
	public boolean isOpen() {
		return open;
	}

	public void setOpen(boolean open) {
		this.open = open;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Loan))
			return false;
		
		Loan lobj = (Loan) obj;
		return lobj.getId() == id;
	}

	@Temporal(TemporalType.DATE)
	@Column
	public Date getDevolutionDate() {
		return devolutionDate;
	}

	public void setDevolutionDate(Date devolutionDate) {
		this.devolutionDate = devolutionDate;
	}
	
}
